﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Bill : System.Web.UI.Page
{    
    private int idMobileNumberTest = 1;  //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    private BillBlo _billBlo=new BillBlo() ;
    private ServiceBlo _serviceBlo=new ServiceBlo() ;
    protected void Page_Load(object sender, EventArgs e)
    {               
        if (!IsPostBack)
        {
            lblError.Visible = false;            
            LoadTopUp(false,"_createDate","DESC","","");
            LoadSpecialRecharge(false,"_createDate","DESC","","");            
            LoadOtherService(false,"_createDate","ASC","","");            
        }
    }                          

    private void LoadTopUp(bool status, string order, string sort, string search_createDate, string search_paymentDate)
    {
        List<BillDto> _listBill = _billBlo.SelectBillAdv(idMobileNumberTest, 1, status, order, sort, search_createDate, search_paymentDate);
        ServiceDto _serviceDto = _serviceBlo.SelectService("TopUp");
        this.LoadGridView(_listBill, _serviceDto.ListServiceDetail,gdvTopUp);
    }
    protected void ddlSortTopUp_SelectedIndexChanged(object sender, EventArgs e)
    {        
        LoadTopUp();
    }
    protected void gdvTopUp_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvTopUp.PageIndex = e.NewPageIndex;
        LoadTopUp();
    }
    protected void btnSearchTopUp_Click(object sender, EventArgs e)
    {
        LoadTopUp();        
    }
    protected void btnShowAllTopup_Click(object sender, EventArgs e)
    {
        ddlBillStatusTopUP.SelectedIndex = 0;
        ddlOrderByTopUp.SelectedIndex = 0;
        ddlSearchTopUp.SelectedIndex = 0;
        ddlSortTopUp.SelectedIndex = 0;
        txtSearchTopUp.Text = "";
        LoadTopUp(false, "_createDate", "DESC", "", "");
    }
    private void LoadTopUp()
    {
        string temp = ddlBillStatusTopUP.SelectedValue.ToString();
        bool status;
        if (temp == "0")
        {
            lblError.Text = "clgt";
            status = false;
        }
        else status = true;
        string order = ddlOrderByTopUp.SelectedValue.ToString();
        string sort = ddlSortTopUp.SelectedValue.ToString();
        string searchDate = txtSearchTopUp.Text;
        if (ddlSearchTopUp.SelectedValue == "CreateDate")
        {
            this.LoadTopUp(status, order, sort, searchDate, "");
        }
        else
            this.LoadTopUp(status, order, sort, "", searchDate);
    }
    
    private void LoadSpecialRecharge(bool status, string order, string sort, string search_createDate, string search_paymentDate)
    {
        List<BillDto> _listBill = _billBlo.SelectBillAdv(idMobileNumberTest, 2, status, order, sort, search_createDate, search_paymentDate);
        ServiceDto _serviceDto = _serviceBlo.SelectService("SpecialRecharge");
        this.LoadGridView(_listBill, _serviceDto.ListServiceDetail, gdvSpecialRecharge);
    }
    protected void gdvSpecialRecharge_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvSpecialRecharge.PageIndex = e.NewPageIndex;
        this.LoadSpecialRecharge();
    }
    protected void ddlSortSpecialRecharge_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadSpecialRecharge();
    }
    protected void btnSearchSpecialRecharge_Click(object sender, EventArgs e)
    {
        this.LoadSpecialRecharge();
    }
    protected void btnShowSpecialRecharge_Click(object sender, EventArgs e)
    {
        LoadSpecialRecharge(false, "_createDate", "DESC", "", "");
        ddlBillStatusSpecialRecharge.SelectedIndex = 0;
        ddlOrderBySpecialRecharge.SelectedIndex = 0;
        ddlSearchSpecialRecharge.SelectedIndex = 0;
        ddlSortSpecialRecharge.SelectedIndex = 0;
        txtSearchSpecialRecharge.Text = "";
    }
    private void LoadSpecialRecharge()
    {
        string temp = ddlBillStatusSpecialRecharge.SelectedValue.ToString();
        bool status;
        if (temp == "0")
        {
            status = false;
        }
        else status = true;
        string order = ddlOrderBySpecialRecharge.SelectedValue.ToString();
        string sort = ddlSortSpecialRecharge.SelectedValue.ToString();
        string searchDate = txtSearchSpecialRecharge.Text;
        if (ddlSearchTopUp.SelectedValue == "CreateDate")
        {
            this.LoadSpecialRecharge(status, order, sort, searchDate, "");
        }
        else
            this.LoadSpecialRecharge(status, order, sort, "", searchDate);
    }
    
    private void LoadOtherService(bool status, string order, string sort, string search_createDate, string search_paymentDate)
    {
        List<BillDto> _listBill = _billBlo.SelectBillOfOtherService(idMobileNumberTest, status, order, sort, search_createDate, search_paymentDate);
        ServiceDto _serviceDto = _serviceBlo.SelectOtherService();
        this.LoadGridView(_listBill, _serviceDto.ListServiceDetail, gdvOtherService);
    }
    protected void ddlSortOtherService_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblError.Text = "11";
        LoadOtherService();
    }
    protected void gdvOtherService_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvOtherService.PageIndex = e.NewPageIndex;
        this.LoadOtherService();
    }
    protected void btnSearchOtherService_Click(object sender, EventArgs e)
    {
        this.LoadOtherService();
    }
    protected void btnShowAllOtherService_Click(object sender, EventArgs e)
    {
        LoadOtherService(false, "_createDate", "DESC", "", "");
        ddlBillStatusOtherService.SelectedIndex = 0;
        ddlOrderByOtherService.SelectedIndex = 0;
        ddlSearchOtherService.SelectedIndex = 0;
        ddlSortOtherService.SelectedIndex = 0;
        txtSearchOtherRecharge.Text = "";
    }
    private void LoadOtherService()
    {
        string temp = ddlBillStatusOtherService.SelectedValue.ToString();
        bool status;
        if (temp == "0")
        {
            status = false;
        }
        else status = true;
        string order = ddlOrderByOtherService.SelectedValue.ToString();
        string sort = ddlSortOtherService.SelectedValue.ToString();
        string searchDate = txtSearchOtherRecharge.Text;
        lblError.Text = "22";
        if (ddlSearchOtherService.SelectedValue == "CreateDate")
        {
            this.LoadOtherService(status, order, sort, searchDate, "");
        }
        else
            this.LoadOtherService(status, order, sort, "", searchDate);
    }

    private void LoadGridView(List<BillDto> _listBill, List<ServiceDetail> _listServiceDetail, GridView gdv)
    {
        var joined = from item1 in _listBill
                     join item2 in _listServiceDetail
                     on item1.Id_SD equals item2.Id_SD into grouping
                     from item2 in grouping.DefaultIfEmpty()
                     select new
                     {
                         item1.Id_Bill,
                         item1.Id_SD,
                         item1.CreateDate,
                         item1.BillStatus,
                         item1.PaymentDate,
                         item2.Total,
                         item2.Denomination,
                         item2.Description,
                         item2.Discount,
                         item2.DurationDate,
                         item2.DurationTime,
                         item2.PackageName,

                     };
        gdv.DataSource = joined.ToList();
        gdv.DataBind();

    }
    
    protected void gdvTopUp_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id_Bill = int.Parse(gdvTopUp.DataKeys[e.RowIndex].Value.ToString());
        _billBlo.UpdatePaidBill(id_Bill);
        LoadTopUp(true, "_paymentDate", "DESC", "", "");
    }
    protected void gdvSpecialRecharge_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id_Bill = int.Parse(gdvSpecialRecharge.DataKeys[e.RowIndex].Value.ToString());
        _billBlo.UpdatePaidBill(id_Bill);
        LoadSpecialRecharge(true, "_paymentDate", "DESC", "", "");
    }
    protected void gdvOtherService_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id_Bill = int.Parse(gdvOtherService.DataKeys[e.RowIndex].Value.ToString());
        _billBlo.UpdatePaidBill(id_Bill);
        LoadOtherService(true, "_paymentDate", "DESC", "", "");
    }
    
    protected void rbtnAllSevice_CheckedChanged(object sender, EventArgs e)
    {
        if (rbtnAllSevice.Checked)
        {
            panelTopUp.Visible = true;
            panelSpecialRecharge.Visible = true;
            panelOtherService.Visible = true;
        }
        else if (rbtnTopUp.Checked)
        {
            panelTopUp.Visible = true;
            panelSpecialRecharge.Visible = false;
            panelOtherService.Visible = false;
        }
        else if (rbtnSpecialRecharge.Checked)
        {
            panelTopUp.Visible = false;
            panelSpecialRecharge.Visible = true;
            panelOtherService.Visible = false;
        }
        else if (rbtnOther.Checked)
        {
            panelTopUp.Visible = false;
            panelSpecialRecharge.Visible = false;
            panelOtherService.Visible = true;
        }
    }
}